<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>使用 QVariant 传递指针   的妙用 | Vico的个人博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="今天终于解决了！！！！！！ 在 QTreeWidget 与 QStackedWidget 连接时 之前一直用一个结构体 存储 QTreeWidgetitem 与 QStackedWidget 中添加的QWidget字符串 1234eg： typdef struct&amp;#123;        QString id[50]; &#x2F;&#x2F;存储名字 用于 QStackedWidget 检索下">
<meta property="og:type" content="article">
<meta property="og:title" content="使用 QVariant 传递指针   的妙用">
<meta property="og:url" content="https://vicolj.gitee.io/2021/03/08/%E4%BD%BF%E7%94%A8%20QVariant%20%E4%BC%A0%E9%80%92%E6%8C%87%E9%92%88%E7%9A%84%E5%A6%99%E7%94%A8/index.html">
<meta property="og:site_name" content="Vico的个人博客">
<meta property="og:description" content="今天终于解决了！！！！！！ 在 QTreeWidget 与 QStackedWidget 连接时 之前一直用一个结构体 存储 QTreeWidgetitem 与 QStackedWidget 中添加的QWidget字符串 1234eg： typdef struct&amp;#123;        QString id[50]; &#x2F;&#x2F;存储名字 用于 QStackedWidget 检索下">
<meta property="og:locale">
<meta property="article:published_time" content="2021-03-08T13:24:41.000Z">
<meta property="article:modified_time" content="2021-03-09T14:51:58.503Z">
<meta property="article:author" content="Vico">
<meta property="article:tag" content="QT">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="Vico的个人博客" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.4.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Vico的个人博客</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/" id="subtitle">Vico的个人博客</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://vicolj.gitee.io"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-使用 QVariant 传递指针的妙用" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2021/03/08/%E4%BD%BF%E7%94%A8%20QVariant%20%E4%BC%A0%E9%80%92%E6%8C%87%E9%92%88%E7%9A%84%E5%A6%99%E7%94%A8/" class="article-date">
  <time class="dt-published" datetime="2021-03-08T13:24:41.000Z" itemprop="datePublished">2021-03-08</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      使用 QVariant 传递指针   的妙用
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <p>今天终于解决了！！！！！！</p>
<p>在 QTreeWidget 与 QStackedWidget 连接时 之前一直用一个结构体 存储 QTreeWidgetitem 与 QStackedWidget 中添加的QWidget字符串</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">eg： typdef struct&#123;</span><br><span class="line">        QString id[50]; &#x2F;&#x2F;存储名字 用于 QStackedWidget 检索下标</span><br><span class="line">        int index[50]； &#x2F;&#x2F;通过index来访问 QStackedWidget 页面</span><br><span class="line">    &#125;TreeBindWidget;</span><br></pre></td></tr></table></figure>

<p>具体用法：<br>    1.在QWidget创建时 规定一个字符id 后续用这个id来访问 QStackedWidget<br>    2.当QTreeWidget 中项目被点击时会发送信号，我们来connect一下</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">connect(treeWidget,SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(onitemClicked(QTreeWidgetItem*,int)));</span><br></pre></td></tr></table></figure>


<pre><code>TreeBindWidget treebindwidget;
void Test::onitemClicked(QTreeWidgetItem* treewidgetitem,int index)
&#123;
    if(treewidgetitem-&gt;text() ==  &quot;keys&quot;) //根据这样一个个寻找然后在找到 将 text 绑定的 id  
    &#123;
        for(int i &lt; 0;i &lt; 50;i++)
        &#123;
            if(text == treebindwidget.name[i])
            &#123;
                setcurindex(treebindwidget.id[i]);
            &#125;
        &#125;

    &#125;
&#125;
</code></pre>
<p>…….<br>以上是一个非常不好的例子 如果你也是这样 请接着看我是怎么解决的</p>
<p>​    </p>
<p>核心函数 setData(int column, int role, const QVariant &amp;value)； //第一个参数列数 第二个 角色 一般填写个人role即可 第三个参数是用来在QTreeWidgetItem中添加信息<br>这里我们可以想到 用 QVariant 存储 QWidget 指针不就好了吗！如果你能想到这里 那么是完全正确的 但是很不幸 QVariant 却不能用来传递指针</p>
<p>使用 QVariant 传递指针<br>我这里使用的是 </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">TEST.h</span><br><span class="line">&#x2F;&#x2F;先声明一个共同体</span><br><span class="line"></span><br><span class="line">class Test::Test&#123;</span><br><span class="line">Q_OBJECT</span><br><span class="line">	public:</span><br><span class="line">	Test::Test(QWidget *parant &#x3D; null);</span><br><span class="line">	Test::~Test()&#123;&#125;</span><br><span class="line">	Typdef union &#123;</span><br><span class="line">        unsigned long long address;</span><br><span class="line">&#x2F;*说明一下我为什么使用 long long ，刚开始的时候我跟大多数人一样 使用 unsigned int 定义一个四字节无符号就够了 后来才发现我用的是64位的系统*&#x2F;</span><br><span class="line">        QWidget *point;</span><br><span class="line">	 &#125;TreeBindWidget;</span><br><span class="line">	 </span><br><span class="line"> private:</span><br><span class="line">	QStackWidget *stackWidget;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<pre><code> TEST.cpp

 Test::Test(QWidget *parant)
     :QWidget(Parant)
 &#123;
      //init Qtreewidget
    QTreeWidget *treewidget = new QTreeWidget;

    QTreeWidgetItem *treeroot = new QTreeWidgetItem;
    treeroot-&gt;setText(&quot;root&quot;);

     TreeBindWidget treebingwidget；
    //init QStackWidget

    QWidget *widget  = new QWidget;
    stackWidget = new QStackWidget;
    stackWidget-&gt;addwidget(widget);

    treebingwidget.point = widget；
    treeroot-&gt;setdata(0,QT::urserole,treebingwidget.address);///////////
 
connect(treeWidget,&amp;QTreeWidget::itemClicked,[this](QTreeWidgetItem* treeitem,int index)&#123;
    QVariant variant = treeitem-&gt;data(0,QT::urserole);
    
    TreeBindWidget treebingwidget；
    if(!variant.isavale)
    &#123;
        treebingwidget.address = variant.toull();
        //读取Qwidget地址，由此来实现QStackedwidget的跳转
        QWidget *widget = (QWidget*)treebingwidget.point;
        stackWidget-&gt;setcurrentWidget(widget);//非常完美的解决了
    &#125;
    
&#125;);

 &#125;
</code></pre>
<p>最后编译运行就可以正常跳转啦    相比之前 代码量 还有 逻辑得到了良好的优化<br>接下来就要开始优化 后面QStackedWidget中程序了<br>加油！</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://vicolj.gitee.io/2021/03/08/%E4%BD%BF%E7%94%A8%20QVariant%20%E4%BC%A0%E9%80%92%E6%8C%87%E9%92%88%E7%9A%84%E5%A6%99%E7%94%A8/" data-id="ckm0oasvd0000e8d5d6afdn4w" data-title="使用 QVariant 传递指针   的妙用" class="article-share-link">Share</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/QT/" rel="tag">QT</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2021/03/08/QT-%E7%BF%BB%E8%AF%91-%E6%9C%89%E5%85%B3tr%E7%9A%84%E9%82%A3%E4%BA%9B%E4%BA%8B/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          QT 翻译 有关tr的那些事
        
      </div>
    </a>
  
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/IEC%E8%A7%84%E8%8C%83/" rel="tag">IEC规范</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/QT/" rel="tag">QT</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/IEC%E8%A7%84%E8%8C%83/" style="font-size: 10px;">IEC规范</a> <a href="/tags/QT/" style="font-size: 20px;">QT</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/03/">March 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/03/29/IEC104%E8%A7%84%E8%8C%83/">IEC104规范</a>
          </li>
        
          <li>
            <a href="/2021/03/13/const%20%E4%B8%8E%20constexpr%20%E7%9A%84%E5%8C%BA%E5%88%AB/">(no title)</a>
          </li>
        
          <li>
            <a href="/2021/03/08/QT%20%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6%E7%B3%BB%E7%BB%9F/">QT 日志框架系统</a>
          </li>
        
          <li>
            <a href="/2021/03/08/QT-%E7%BF%BB%E8%AF%91-%E6%9C%89%E5%85%B3tr%E7%9A%84%E9%82%A3%E4%BA%9B%E4%BA%8B/">QT 翻译 有关tr的那些事</a>
          </li>
        
          <li>
            <a href="/2021/03/08/%E4%BD%BF%E7%94%A8%20QVariant%20%E4%BC%A0%E9%80%92%E6%8C%87%E9%92%88%E7%9A%84%E5%A6%99%E7%94%A8/">使用 QVariant 传递指针   的妙用</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 Vico<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.4.1.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>